package com.yixun.lbx.controller.farm;

import com.yixun.controller.BaseController;
import com.yixun.lbx.entity.YxProCompany;
import com.yixun.lbx.entity.YxProProductInfo;
import com.yixun.lbx.service.CompanyService;
import com.yixun.lbx.service.CompanyStaffService;
import com.yixun.lbx.service.ProductInfoServie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/pm")
public class ProductInfoController extends BaseController {
    private final ProductInfoServie productInfoServie;
    private final CompanyService companyService;
    @Autowired
    private CompanyStaffService staffService;
    
    public ProductInfoController(ProductInfoServie productInfoServie, CompanyService companyService) {
        this.productInfoServie = productInfoServie;
        this.companyService = companyService;
    }
    
    /*
     *产品信息列表
     */
    @PostMapping("/getListByPage")
    public HashMap<String, Object> getListByPage(@RequestBody HashMap<String, Object> map) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            map.put("qyId", qyId);
        }
        Integer count = productInfoServie.getCount(map);
        List<YxProProductInfo> productInfos = productInfoServie.getListByPage(map);
        
        map.put("count", count);
        map.put("productInfos", productInfos);
        return map;
    }
    
    /*
     *添加产品信息
     */
    @PostMapping("/addProductInfo")
    public int addBeeHive(@RequestBody HashMap<String, Object> map) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            map.put("qyId", qyId);
        }
        map.put("addUser", this.getSessionUser().getUser_id());
        return productInfoServie.addProductInfo(map);
    }
    
    /*
     *修改产品信息
     */
    @PostMapping("/updateProductInfo")
    public int updateBeeHive(@RequestBody HashMap<String, Object> map) {
        map.put("updateUser", this.getSessionUser().getUser_id());
        String[] cpIds = map.get("cpId").toString().split(",");
        map.put("cpIds", cpIds);
        YxProProductInfo one = productInfoServie.getOneByCpId((String) map.get("cpId"));
        String report = null;
        if (map.get("photo") != null) {
            String photo = one.getPhoto();
            String[] photos = photo.split(",");
            report = one.getReport();
            for (String s : photos) {
                staffService.deleteFile(s);
            }
        }
        if (map.get("report") != null) {
            staffService.deleteFile(report);
        }
        return productInfoServie.updateProductInfo(map);
    }
    
    /*
     *删除产品信息
     */
    @PostMapping("/deleteProductInfo")
    public int deleteBeeHive(@RequestBody HashMap<String, Object> map) {
        map.put("updateUser", this.getSessionUser().getUser_id());
        String[] cpIds = map.get("cpId").toString().split(",");
        map.put("cpIds", cpIds);
        for (String cpId : cpIds) {
            YxProProductInfo one = productInfoServie.getOneByCpId(cpId);
            String photo = one.getPhoto();
            if (photo != null) {
                String[] photos = photo.split(",");
                for (String s : photos) {
                    staffService.deleteFile(s);
                }
            }
            String report = one.getReport();
            if (report != null) {
                staffService.deleteFile(report);
            }
        }
        return productInfoServie.deleteProductInfo(map);
    }
    
    @GetMapping("/getObjByArgs")
    public List<YxProProductInfo> getObjByArgs(YxProProductInfo productInfo) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            productInfo.setQyId(String.valueOf(qyId));
        }
        return productInfoServie.getObjByArgs(productInfo);
    }
    
    private Long getQyId() {
        YxProCompany yxProCompany = new YxProCompany();
        yxProCompany.setCode(this.getSessionUser().getUser_name());
        YxProCompany company = companyService.getCompanyByIdOrNameOrCode(yxProCompany);
        if (company != null) {
            return company.getQyId();
        } else {
            return 0L;
        }
    }
    
    /*搜索*/
    @RequestMapping("/getSearchListByPage")
    public HashMap<String, Object> getSearchListByPage(@RequestBody HashMap<String, String> map) {
        HashMap<String, Object> hashMap = new HashMap<>();
        map.put("qyId", String.valueOf(this.getQyId()));
        List<YxProProductInfo> companies = productInfoServie.getSearchListByPage(map);
        hashMap.put("companies", companies);
        hashMap.put("count", companies.size());
        return hashMap;
    }
}
